﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using Ecopsy.DataTier;
using AgencyEntityLib.Context;


namespace AgencyEntityLib.BaseEntity
{
    public class wsTask :EntitySingle 
    {

        public wsTask()
        {

            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }

        //[WebMethod(EnableSession = true)]
        //public TaskList GetTaskList(string Where)
        //{
        //    TaskList list = new TaskList(){ ParamList = Where };
        //    list.Load();
        //    return list;    
        //}


       
        public TaskList GetTaskList(string Where, DateTime dtStart, DateTime dtFinish)
        {
            TaskList.sqlLoadStatment WhereStatment = new TaskList.sqlLoadStatment();
            WhereStatment.dtStart = dtStart;
            WhereStatment.dtFinish = dtFinish;
            WhereStatment.Lexeme = Where;
            return GetTaskList(WhereStatment);
        }

      
        public TaskList GetTaskList(TaskList.sqlLoadStatment WhereStatment)
        {
            TaskList list = new TaskList();
            list.SqlStatment = WhereStatment;
            list.Load();
            return list;
        }

     
        public Task GetNewTask(string ID)
        {
            if (!CacheTools.IsCacheObject(ID))
            {
                Task p = new Task();
                p.ID = ID;
                CacheTools.SetCacheObject(ID, p);
                return p;
            }

            return (Task)CacheTools.GetCacheObect(ID);

        }

 
        public Task GetTask(string ID)
        {
            if (!CacheTools.IsCacheObject(ID))
            {
                Task p = new Task();
                p.ID = ID;
                p.Load();
                CacheTools.SetCacheObject(ID, p);
                return p;
            }
            return (Task)CacheTools.GetCacheObect(ID);
        }


        public Task SaveTask(Task _Task)
        {
            if (_Task.ID.IndexOf(AgencyLib_GlobalConstants.qPrefNEW_RECORD) > -1)//режим вставки?
            {
                string s = _Task.ID;
                _Task.ID = _Task.ID.Replace(AgencyLib_GlobalConstants.qPrefNEW_RECORD, "");
                _Task.Save();
                return _Task;
            }
            if (_Task.State == EntityState.Deleted)
            {
                _Task.Save();
                CacheTools.ResetCache(_Task.ID);
            }
            else
            {
                _Task.Save(AppContext.GetSC().ConnectionString);
            }

            return _Task;

        }


    }
}
